<template>
    <div class="enquiry-quotation">
        <van-cell-group>
            <van-cell title="报价生效时间">
                <p class="fix-time-style">
                    <span @click="watchTimeSection = 'start'">{{ timeSection.start }}</span> 至 <span
                    @click="watchTimeSection = 'end'">{{ timeSection.end }}</span>
                </p>
            </van-cell>
            <van-cell title="报价生效地区" :value="region.name" is-link @click="region.show = true"/>
        </van-cell-group>
        <van-cell-group>
            <van-field label="分类名称" :value="params.item.name" readonly class="classify-fix-value"/>
            <van-field v-for="(item, index) in forma" :key="index" v-model="item.price" :label="item.name"
                       placeholder="请输入相应数值" input-align="right"/>
        </van-cell-group>
        <van-button type="primary" text="确定" class="fix-button-wrap full-width" @click="eventClickSubmiteFormOperate"/>
        <van-popup v-model="timeSection.show" position="bottom">
            <van-datetime-picker v-model="timeSection.value" @cancel="timeSection.show = false"
                                 @confirm="watchTimeSection = 'set'" type="datetime"/>
        </van-popup>
        <van-popup v-model="region.show" position="bottom">
            <van-area :value="region.code" :area-list="region.areaList" @cancel="region.show = false"
                      @confirm="eventClickSelectOperate"/>
        </van-popup>
    </div>
</template>

<script>
    import {Cell, CellGroup, List, Popup, DatetimePicker, Area, Field, Button} from 'vant';
    import {Message} from "element-ui"
    import AreaList from "../../../src/libs/js/area.js";

    export default {
        props: [
            'params'
        ],
        components: {
            [List.name]: List,
            [Cell.name]: Cell, [Field.name]: Field, [CellGroup.name]: CellGroup,
            [Popup.name]: Popup, [DatetimePicker.name]: DatetimePicker,
            [Area.name]: Area,
            [Message.name]: Message,
            [Button.name]: Button
        },
        data() {
            return {
                watchTimeSection: '',
                watchRegion: '',
                forma: [
                    {
                        price: 0, name: 'kg'
                    }
                ],
                timeSection: {
                    show: false, value: new Date(),
                    start: this.$moment().startOf('month').format('MM-DD HH:mm'),
                    ostart: this.$moment().startOf('month').format('X'),
                    end: this.$moment().endOf('month').format('MM-DD HH:mm'),
                    oend: this.$moment().startOf('month').format('X')
                },
                region: {
                    show: false,
                    name: '全国', code: 0,
                    areaList: {}
                }
            }
        },
        mounted() {
            // console.log(this.params);

            this.region.areaList = this.params.areaList;
            this.region.name = this.params.region.name;
            this.region.code = this.params.region.code;

            for (let x of this.params.item.oda.danwei) {
                if (x.danweiming == 'kg') {
                    this.forma[0].price = x.price;
                    break;
                }
            }
            // this.forma[0].price = this.$route.params

            // if(this.$route.params.item.oda.danwei.length > 0) {
            //     let danwei = this.$route.params.item.oda.danwei;

            //     for(let i in danwei) {
            //         if(i == 0) {
            //             this.forma[i].price = danwei[i].price;
            //             this.forma[i].name = danwei[i].danweiming;
            //         } else {
            //             this.forma.push({
            //                 price: danwei[i].price, name: danwei[i].danweiming
            //             });
            //         }
            //     }
            // }

            this.reqeustQuotedPriceDetials();
        },
        methods: {
            eventClickSelectOperate(params) {
                this.region.show = false;
                this.region.name = params[0].name + '-' + params[2].name;
                this.region.code = params[2].code;
            },
            reqeustQuotedPriceDetials() {
                var url = '/api/home/garbageprice/getone',
                    params = {
                        id: this.$route.params.id
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    console.log(res);
                });
            },
            requestSettingQuotedPrice() {
                // console.log(this.$route.params);

                var url = '/api/home/GarbagePrice/change',
                    params = {
                        id: this.params.item.id,
                        // name: this.params.item.name,
                        start_time: this.timeSection.ostart,
                        end_time: this.timeSection.oend,
                        danwei: JSON.stringify(this.forma),
                        // weight: this.forma.weight,
                        // number: this.forma.number,
                        garbageid: this.params.item.pgalist,
                        regionz: this.region.code,
                        token: this.$store.state.options.user.token
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    if (res.code == 200) {
                        Message.success("修改成功")

                        this.params.price = this.forma[0].price;
                        this.$emit('onEditEvent', this.params);
                    } else {
                        Message.error("修改成功")
                    }
                });
            },
            eventClickSubmiteFormOperate() {

                this.requestSettingQuotedPrice();

            }
        },
        watch: {
            watchTimeSection(nVal, oVal) {
                // console.log(nVal, oVal);

                if (nVal == 'start' || nVal == 'end') {
                    this.timeSection.show = true;
                    this.timeSection.value = (nVal == 'start') ? new Date(this.timeSection.ostart) : new Date(this.timeSection.oend);
                    this.watchTimeSection = 'is-' + nVal;
                }

                if (nVal == 'set' && oVal == 'is-start') {
                    this.timeSection.start = this.$moment(this.timeSection.value).format('MM-DD HH:mm');
                    // this.timeSection.ostart = this.$moment(this.timeSection.value).format('YYYY-MM-DD HH:mm');
                    this.timeSection.ostart = this.$moment(this.timeSection.value).format('X');
                    this.timeSection.show = false;
                } else if (nVal == 'set' && oVal == 'is-end') {
                    this.timeSection.end = this.$moment(this.timeSection.value).format('MM-DD HH:mm');
                    // this.timeSection.oend = this.$moment(this.timeSection.value).format('YYYY-MM-DD HH:mm');
                    this.timeSection.oend = this.$moment(this.timeSection.value).format('X');
                    this.timeSection.show = false;
                }
            },
            watchRegion(nVal, oVal) {
                if (nVal == 'show') {
                    this.region.show = true;
                    this.watchRegion = 'none';
                }
            },
            params() {
                this.region.areaList = this.params.areaList;
                this.region.name = this.params.region.name;
                this.region.code = this.params.region.code;

                for (let x of this.params.item.oda.danwei) {
                    if (x.danweiming == 'kg') {
                        this.forma[0].price = x.price;
                        break;
                    }
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .page-top {
        position: relative;
        padding: 0rem 1rem;
        border-bottom: 0.4rem solid #f4f4f4;
    }

    .quoted-price-time {
        padding: 1rem 0rem;
        border-bottom: 0.1rem solid #f4f4f4;
    }

    .offer-list-wrap {
        padding: 0rem 1rem;
        border-bottom: 0.4rem solid #f4f4f4;

        .title {
            padding: 1rem 0rem 1rem;
            display: flex;
            border-bottom: 0.1rem solid #f4f4f4;

            p {
                font-size: 1.4rem;

                span:first-child {
                    color: #ff0000;
                }
            }

            p:first-child {
                flex: 1;
            }

            p:last-child {
                margin-left: 1rem;
            }
        }

        .button {
            text-align: right;
            font-size: 1.3rem;
            padding: 1rem 0rem;
            display: flex;
            flex-direction: row-reverse;

            p {
                &:first-child {
                    margin-left: 1.6rem;
                }

                img {
                    width: 1.7rem;
                    vertical-align: middle;
                    margin-right: 0.5rem;
                }

                span {
                    position: relative;
                    top: 1.5px;
                }
            }
        }
    }

    .goback-button {
        background-color: #00cc00;
        padding: 1rem;
        color: #ffffff;
        margin-bottom: 1rem;
    }

    .van-cell__title {
        flex: none;
    }

    .van-cell__value {
        flex: 1;
    }

    .fix-time-style {
        text-align: right;
    }

    .fix-button-wrap {
        position: absolute;
        bottom: 0rem;
        background-color: #00cc00;
        font-size: 1.7rem;
    }
</style>
